package com.tianqu.tms.www.transfer.action;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.*;
import com.tianqu.tms.www.transfer.actionform.CheckConfirmActionForm;
import com.tianqu.tms.www.transfer.viewhelper.CheckConfirmViewHelper;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工商验证查询结果页
 * 
 * @author Administrator
 * 
 */
public class IndustryConfirmSearchResultAction extends TmsAction {

	private Logger logger = Logger
			.getLogger(IndustryConfirmSearchResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		CheckConfirmActionForm inputForm = (CheckConfirmActionForm) form;
		CheckConfirmViewHelper view = new CheckConfirmViewHelper();
		String serial = inputForm.getSerial();
		logger.debug("前台获取流水号：" + serial);
		String carLicense = inputForm.getCarLicense();
		logger.debug("前台获取车牌号码：" + carLicense);
		if (LicensePlateConfigConstant.defLicensePlate.equals(carLicense)) {
			carLicense = "";
		}
		Codes code = Codes.getInstance();
		if ((serial == null || "".equals(serial))
				&& (carLicense == null || "".equals(carLicense))) {
			setErrorsToRequest(request, TmsConstants.IM005, null);
			return mapping.getInputForward();
		} else {
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.getAcceptInfoBySerial(serial, carLicense);
			if (accept != null) {
				if (accept.getZy_printTime() != null
						&& !"".equals(accept.getZy_printTime())
						&& accept.getStatus() != 16) {

					view.setSerial(accept.getSerial());
					view.setStatus(code.getItemValueByItemIdAndItemCode(
							Codes.ACCEPT_STATUS, accept.getStatus()));
					view.setLicensePlate(accept.getLicensePlate());
					view.setVin(accept.getVin());
					view.setCarOwner(accept.getCarOwner());

					if (accept.getMarketType() == 1) {
						Car car = Car.getInstance();
						car = car.findCarBySerial(accept.getBasicCarId());
						view.setModeCode(car.getVendorCode()
								+ car.getModelCode());
						view.setRegisterNo(car.getRegisterCardNo());
						view.setCy_cardType(code
								.getItemValueByItemIdAndItemCode(
										Codes.VEHICLE_TYPE, car.getCarType()));
						if (car.getCarOwnerIdCardNo() != null
								&& !"".equals(car.getCarOwnerIdCardNo())) {
							view.setSfOrDmNo(car.getCarOwnerIdCardNo());
						} else {
							view.setSfOrDmNo(car.getOwnerOrgNo());
						}
						view.setContectPhone(car.getOwnerPhone());
					}
					if (accept.getMarketType() == 2) {
						TransferPersonalCarInfo person = new TransferPersonalCarInfo();
						person = person.getPersonalCarInfoBySerial(accept
								.getBasicCarId());
						view.setModeCode(person.getVendorCode()
								+ person.getModelCode());
						view.setRegisterNo(person.getRegisterCardNo());
						view
								.setCy_cardType(code
										.getItemValueByItemIdAndItemCode(
												Codes.VEHICLE_TYPE, person
														.getCarType()));
						if (person.getCarOwnerIdCardNo() != null
								&& !"".equals(person.getCarOwnerIdCardNo())) {
							view.setSfOrDmNo(person.getCarOwnerIdCardNo());
						} else {
							view.setSfOrDmNo(person.getOwnerOrgNo());
						}
						view.setContectPhone(person.getOwnerPhone());
					}
					if (accept.getSerial() != null
							&& !"".equals(accept.getSerial())) {
						TransferProcessHistory his = new TransferProcessHistory();
						List list = his
								.loadtHistoryBySerial(accept.getSerial());
						if (list != null && list.size() > 0) {
							for (int i = 0; i < list.size(); i++) {
								TransferProcessHistory getNewNTMessage = (TransferProcessHistory) list
										.get(i);
								if (getNewNTMessage.getBusinessType() == 32) {
									view.setTgBsFlag("2");
									view.setReason(getNewNTMessage.getReason());
									break;
								}
							}
						}
						view.setLcList(list);
					}
					uc.setAttribute("serial", accept.getSerial());
				} else {
					setErrorsToRequest(request, TmsConstants.IM006, null);
					return mapping.getInputForward();
				}
			} else {
				setErrorsToRequest(request, TmsConstants.IM006, null);
				return mapping.getInputForward();
			}
		}
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
